package com.spark.tags;

import java.util.Map;

import com.spark.core.SparkContext;
import com.spark.core.XMap;

public class Tree extends ViewComponent {
	
	private static final String DEFAULT_TEMPLATES = "tree.ftl";
	
	public static final String PROPERTITY_RENDERER = "renderer";
	public static final String PROPERTITY_FIELD_ID = "idField";
	public static final String PROPERTITY_SHOWCHECKBOX = "showCheckBox";
	public static final String PROPERTITY_FIELD_PARENT = "parentField";
	public static final String PROPERTITY_FIELD_DISPLAY = "displayField";
	public static final String PROPERTITY_FIELD_EXPAND = "expandField";
	public static final String PROPERTITY_FIELD_ICON = "iconField";
	public static final String PROPERTITY_EXPAND_ALL = "expandAll";
	
	public static final String PROPERTITY_FIELD_CHECKED = "checkField";
	public static final String PROPERTITY_FIELD_SEQUENCE = "sequenceField";
	
	private static final String DEFAULT_ID_FIELD = "id";
	private static final String DEFAULT_PARENT_FIELD = "pid";
	private static final String DEFAULT_DISPLAY_FIELD = "name";
	private static final String DEFAULT_EXPAND_FIELD ="expanded";
	private static final String DEFAULT_SEQUENCE_FIELD = "sequence";
	private static final String DEFAULT_CHECKED_FIELD = "checked";
	private static final String DEFAULT_ICON_FIELD = "icon";
	
	private static final String DEFAULT_CLASSNAME = "s_tree";
	
	public static final String CONFIG_CONTEXT = "context";
	
	public String getTemplate(){
		return DEFAULT_TEMPLATES;
	}
	
	protected String getDefaultClass(XMap view,SparkContext context){
		String className = super.getDefaultClass(view, context);
		StringBuilder sb = new StringBuilder();
		sb.append(className);
		if(!"".equals(className.trim()))sb.append(" ");
		sb.append(DEFAULT_CLASSNAME);
		return sb.toString();
	}
	
	public void init(XMap view, SparkContext context) throws Exception{
		super.init(view, context);
		
//		String size = "";
//		/** Width属性**/
//		String width = view.getString(PROPERTITY_WIDTH, "");
//		if(!"".endsWith(width)) {
//			size += "width:"+width+"px;";
//			context.addJsonConfig(PROPERTITY_WIDTH, width);
//		}
//		/** Height属性**/
//		String height = view.getString(PROPERTITY_HEIGHT, "");
//		if(!"".endsWith(height)) {
//			size += "height:"+height+"px;";
//			context.addJsonConfig(PROPERTITY_HEIGHT, height);
//		}
//		
//		context.put("size", size);
//		context.putConfig(PROPERTITY_BINDTARGET, view.getString(PROPERTITY_BINDTARGET));
		
		if(context.getContextPath()!=null) context.addJsonConfig(CONFIG_CONTEXT,context.getContextPath()+"/");
		context.addJsonConfig(PROPERTITY_FIELD_DISPLAY, view.getString(PROPERTITY_FIELD_DISPLAY,DEFAULT_DISPLAY_FIELD));
		
		
		Boolean expandAll = view.getBoolean(PROPERTITY_EXPAND_ALL);
		if(expandAll!= null) context.addJsonConfig(PROPERTITY_EXPAND_ALL, expandAll);
		String renderer = view.getString(PROPERTITY_RENDERER);
		if(renderer!=null)context.addJsonConfig(PROPERTITY_RENDERER, renderer);
		
		context.addJsonConfig(PROPERTITY_FIELD_ID, view.getString(PROPERTITY_FIELD_ID,DEFAULT_ID_FIELD));
		context.addJsonConfig(PROPERTITY_FIELD_PARENT, view.getString(PROPERTITY_FIELD_PARENT, DEFAULT_PARENT_FIELD));
		context.addJsonConfig(PROPERTITY_SHOWCHECKBOX,  view.getBoolean(PROPERTITY_SHOWCHECKBOX, false));
		context.addJsonConfig(PROPERTITY_FIELD_CHECKED, view.getString(PROPERTITY_FIELD_CHECKED,DEFAULT_CHECKED_FIELD));
		context.addJsonConfig(PROPERTITY_FIELD_EXPAND, view.getString(PROPERTITY_FIELD_EXPAND, DEFAULT_EXPAND_FIELD));
		context.addJsonConfig(PROPERTITY_FIELD_ICON, view.getString(PROPERTITY_FIELD_ICON, DEFAULT_ICON_FIELD));
		context.addJsonConfig(PROPERTITY_FIELD_SEQUENCE, view.getString(PROPERTITY_FIELD_SEQUENCE, DEFAULT_SEQUENCE_FIELD));
	}
	
	@SuppressWarnings("unchecked")
	protected String build(SparkContext context){
		Map map = context.getMap();
		StringBuilder sb = new StringBuilder();
		sb.append("<div style=\"width:").append(get(map,"width")).append("px;height:").append(get(map,"height")).append("px;overflow:auto;").append(get(map,"style")).append("\" class=\"").append(get(map,"className")).append("\" id=\"").append(get(map,"id")).append("\"><div stype=\"tree.body\"></div></div>\r");
		sb.append("<script>\r");
		sb.append("	new Spark.Tree(").append(get(map,"config")).append(");\r");
		sb.append("</script>\r");
		return sb.toString();
	}
	
}
